//------------------------------------------------------------------------------
#include "CChatThread.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
CChatThread *CChatThread::instance=NULL;
//------------------------------------------------------------------------------
CChatThread *CChatThread::getInstance(void)
{
	if(instance == NULL)
	{
		instance=new CChatThread();
	}
	
	return instance;
}
//------------------------------------------------------------------------------
CChatThread::CChatThread(QObject *parent) : QThread(parent)
{
	db=QSqlDatabase::cloneDatabase(CMySQL::getInstance()->getDatabase(), "chatThread");
	db.open();
}
//------------------------------------------------------------------------------
void CChatThread::run(void)
{
	QString requete;
	QSqlQuery *query=new QSqlQuery(db);
	int lastId=0;
	
	isThreadStopped=false;

	while(!isThreadStopped)
	{	
		requete="SELECT id, dateMessage, heureMessage, qui, message ";
		requete+="FROM chat ";
		requete+="WHERE id > "+QString::number(lastId)+" ";
		requete+="ORDER BY id";

		if(query->exec(requete))
		{
			emit addLine(query, &lastId);
		}
		
		sleep(2);
	}

	delete query;
}
//------------------------------------------------------------------------------